Stap 4: Software
Deze code is afhankelijk van de ITEAD WeeESP8266-bibliotheek. Begin met downloaden en installeren: https://github.com/itead/ITEADLIB_Arduino_WeeESP82...
Deze code is gebaseerd op het voorbeeld van de TCPClientSingle uit de WeeESP8266-bibliotheek en deze e-mail tutorial uit de Arduino Playground. Het is ontworpen om continu verbonden zijn met WiFi; in het volgende voorbeeld doet niets met energiebeheer en moet een vlezige batterij en zonnepaneel te werken.
Als u vond deze tutorial, gelieve de Arachnio steunen op Kickstarter: https://www.kickstarter.com/projects/logos-electro/arachnio
<p>#include "ESP8266.h"</p><p>#define SSID "mySSID" #define PASSWORD "myPassword" #define HOST_NAME "mail.mydomain.com" #define HOST_IP "1.2.3.4" #define HOST_PORT (25) #define FROM "<me #define TO "<you PIRPIN 12</p><p>ESP8266 wifi(Serial1);</p><p>int sendMail (void);</p><p>void setup(void) { Serial.begin(9600); Serial.print("setup begin\r\n"); Serial.print("FW Version:"); Serial.println(wifi.getVersion().c_str()); if (wifi.setOprToStationSoftAP()) { Serial.print("to station + softap ok\r\n"); } else { Serial.print("to station + softap err\r\n"); } if (wifi.joinAP(SSID, PASSWORD)) { Serial.print("Join AP success\r\n"); Serial.print("IP:"); Serial.println( wifi.getLocalIP().c_str()); } else { Serial.print("Join AP failure\r\n"); } if (wifi.disableMUX()) { Serial.print("single ok\r\n"); } else { Serial.print("single err\r\n"); } Serial.print("setup end\r\n"); pinMode(PIRPIN, INPUT); } void loop(void) { if (digitalRead(PIRPIN) == HIGH) { delay(500); if (digitalRead(PIRPIN) == HIGH) { sendMail(); } } delay(30000); }</p><p>int sendMail (void) { char buf[256] = {0}; char rbuf[32] = {0}; if (wifi.createTCP(HOST_NAME, HOST_PORT)) { Serial.print("create tcp ok\r\n"); } else { Serial.print("create tcp err\r\n"); return 0; } strcat(buf, "helo "); strcat(buf, HOST_IP); wifi.send((const uint8_t*)buf, strlen(buf)); if (!wifi.recv((uint8_t*)rbuf, 32)) return 0; buf[0] = '\0'; // reset buf for the next set of strcat calls strcat(buf, "To: You "); strcat(buf, TO); strcat(buf, "\r\nFrom: Me "); strcat(buf, FROM); strcat(buf, "\r\nSubject: Something moved!\r\n\r\n"); strcat(buf, "A moving thing was detected!\r\n.\r\n"); wifi.send((const uint8_t*)buf, strlen(buf)); if (!wifi.recv((uint8_t*)rbuf, 32)) return 0; buf[0] = '\0'; // reset buf for the next set of strcat calls strcat(buf, "QUIT\r\n"); wifi.send((const uint8_t*)buf, strlen(buf)); if (!wifi.recv((uint8_t*)rbuf, 32)) return 0; if (wifi.releaseTCP()) { Serial.print("release tcp ok\r\n"); } else { Serial.print("release tcp err\r\n"); } return -1; }</p>