Maintenance: Project Hub will be unavailable on Wednesday 25 (9AM to 6PM CET) while we deploy critical improvements
IoT with ESP8266
Remotely switch things and read data using ESP8266 Arduino and your website.
Components and supplies
1
ESP8266 ESP-01
1
Arduino UNO
Project description
Code
iot_wifi
c_cpp
1// Simple IoT using ESP8266 and your website 2// for more inf see moty22.co.uk 3 4#include <SoftwareSerial.h> 5SoftwareSerial wifi(6, 5); // RX, TX for ESP8266 6 7 8 int analog=0, i=0; 9 String msg, host, d1; 10 11const int str_len = 100; 12char str_data[str_len]; 13const char end_c = '*', start_c = '%'; //marking the data part of the website reply 14 15void setup() 16{ 17 18 pinMode(7,OUTPUT); 19 pinMode(3,INPUT_PULLUP); 20 wifi.begin(9600); //software serial 21 Serial.begin(9600); 22 // wifi.listen(); 23 24 wifi.println("AT+RST"); //reset wifi 25 delay(2000); 26 wifi.println("AT+CWMODE=1"); //station mode 27 delay(1000); 28 //connect to AP, ADD YOUR ROUTER SSID and PASSWORD 29 wifi.println("AT+CWJAP=\\"SSID\\",\\"PASSWORD\\""); 30 delay(10000); 31 32} 33 34void loop() 35{ 36 ++i; 37 if(i=2000){ 38 analog = analogRead(A0); 39 if(digitalRead(3)) {d1 = "OFF";} else {d1 = "ON";} 40 41 //GET request of a website page a31.php 42 msg = "GET /a31.php?v1=" + String(highByte(analog)) + "&v2=" + String(lowByte(analog)) + "&v3=" + d1 + " HTTP/1.1"; 43 host = "HOST: moty22.co.uk"; //change to your website address 44 45 wifi.println("AT+CIPSTART=\\"TCP\\",\\"moty22.co.uk\\",80"); //change to your website address 46 delay(1000); 47 wifi.println("AT+CIPSEND=" + String(msg.length() + host.length() + 6)); 48 delay(1000); 49 50 wifi.println(msg); 51 wifi.println(host); 52 wifi.println(""); 53 i=0; 54 } 55 //get reply from website 56 get_str(); 57 Serial.println(str_data); 58 if(find(str_data,"output=ON")){digitalWrite(7, HIGH); Serial.println("output=ON");} 59 if(find(str_data,"output=OFF")){digitalWrite(7, LOW); Serial.println("output=OFF");} 60 delay(10000); 61 62} 63 64boolean find(String string, String toFind){ 65 if(string.indexOf(toFind)>=0) return true; 66 67 return false; 68} 69 70void get_str(){ 71 int i = 0; 72 bool begin_str=0; 73 74 char c = read_c(); 75 while(c != end_c){ 76 if(i >= str_len) break; 77 if(c == start_c) { begin_str=1;} 78 if (begin_str) { 79 str_data[i] = c; 80 i++; 81 } 82 c = read_c(); 83 84 } 85 str_data[i] = '\\0'; 86} 87 88char read_c(){ 89 while(!wifi.available()); 90 return wifi.read(); 91} 92
iot_wifi
c_cpp
1// Simple IoT using ESP8266 and your website 2// for more inf see moty22.co.uk 3 4#include 5 <SoftwareSerial.h> 6SoftwareSerial wifi(6, 5); // RX, TX for ESP8266 7 8 9 10 int analog=0, i=0; 11 String msg, host, d1; 12 13const int str_len 14 = 100; 15char str_data[str_len]; 16const char end_c = '*', start_c = '%'; //marking 17 the data part of the website reply 18 19void setup() 20{ 21 22 pinMode(7,OUTPUT); 23 24 pinMode(3,INPUT_PULLUP); 25 wifi.begin(9600); //software serial 26 Serial.begin(9600); 27 28 // wifi.listen(); 29 30 wifi.println("AT+RST"); //reset wifi 31 delay(2000); 32 33 wifi.println("AT+CWMODE=1"); //station mode 34 delay(1000); 35 //connect 36 to AP, ADD YOUR ROUTER SSID and PASSWORD 37 wifi.println("AT+CWJAP=\\"SSID\\",\\"PASSWORD\\""); 38 39 delay(10000); 40 41} 42 43void loop() 44{ 45 ++i; 46 if(i=2000){ 47 48 analog = analogRead(A0); 49 if(digitalRead(3)) {d1 = "OFF";} else 50 {d1 = "ON";} 51 52 //GET request of a website page a31.php 53 msg 54 = "GET /a31.php?v1=" + String(highByte(analog)) + "&v2=" + String(lowByte(analog)) 55 + "&v3=" + d1 + " HTTP/1.1"; 56 host = "HOST: moty22.co.uk"; //change 57 to your website address 58 59 wifi.println("AT+CIPSTART=\\"TCP\\",\\"moty22.co.uk\\",80"); 60 //change to your website address 61 delay(1000); 62 wifi.println("AT+CIPSEND=" 63 + String(msg.length() + host.length() + 6)); 64 delay(1000); 65 66 wifi.println(msg); 67 68 wifi.println(host); 69 wifi.println(""); 70 i=0; 71 } 72 //get 73 reply from website 74 get_str(); 75 Serial.println(str_data); 76 if(find(str_data,"output=ON")){digitalWrite(7, 77 HIGH); Serial.println("output=ON");} 78 if(find(str_data,"output=OFF")){digitalWrite(7, 79 LOW); Serial.println("output=OFF");} 80 delay(10000); 81 82} 83 84boolean 85 find(String string, String toFind){ 86 if(string.indexOf(toFind)>=0) return true; 87 88 89 return false; 90} 91 92void get_str(){ 93 int i = 0; 94 bool begin_str=0; 95 96 97 char c = read_c(); 98 while(c != end_c){ 99 if(i >= str_len) break; 100 101 if(c == start_c) { begin_str=1;} 102 if (begin_str) { 103 str_data[i] 104 = c; 105 i++; 106 } 107 c = read_c(); 108 109 } 110 str_data[i] 111 = '\\0'; 112} 113 114char read_c(){ 115 while(!wifi.available()); 116 return wifi.read(); 117} 118
Downloadable files
wifi_iot
wifi_iot

Comments
Only logged in users can leave comments