Components and supplies
NEMA 17 Stepper Motor
Arduino 4 Relays Shield
W5100 Ethernet Network Module
DHT11 Temperature & Humidity Sensor (4 pins)
Arduino Nano R3
Project description
Code
Home connected
c_cpp
1#include "EtherCard.h" 2#include "ArduinoJson.h" 3#include "string.h" 4#include "DHT.h" 5 6#define DHTTYPE DHT11 7DHT dht(9, DHTTYPE); 8//ethernet interface mac address, must be unique on the LAN 9static byte mymac[] = {0x74,0xDE,0x2B,0x83,0x41,0x79}; 10static byte myip[] = { 192,168,1,44}; 11byte Ethernet::buffer[500]; 12BufferFiller bfill; 13static uint32_t timer; 14static uint32_t timers; 15const char website[] PROGMEM = "www.domaine.com"; 16String retour =""; 17int emp=0; 18char json[80]; 19int state=0; 20int con=0; 21 22static void my_callback (byte status, word off, word len) { 23Ethernet::buffer[off+300] = 0; 24Serial.println("Contenu HTML"); 25String tout = (const char*) Ethernet::buffer + off; 26int pos = tout.indexOf("plain"); 27String var = tout.substring(pos+9); 28retour=var; 29int rotation; 30int posi = var.indexOf(","); 31if(retour.indexOf(",")>0){ 32retour = var.substring(0, posi); 33rotation = var.substring(posi+1).toInt(); 34} 35Serial.println(tout); 36Serial.println("La variable de commande"); 37Serial.println(var); 38Serial.println(rotation); 39if (retour == "alc"){ 40digitalWrite(7, HIGH); 41emp=0; 42} else if(retour == "elc"){ 43digitalWrite(7, LOW); 44emp=0; 45} else if (retour == "dv"){ 46digitalWrite(6, HIGH); 47emp=0; 48} else if(retour == "av"){ 49digitalWrite(6, LOW); 50emp=0; 51} else if (retour == "mch" && emp==0){ 52digitalWrite(10, HIGH); 53delay(rotation*100); 54digitalWrite(10, LOW); 55emp=1; 56} else if(retour == "ach"){ 57emp=0; 58digitalWrite(10, LOW); 59} 60state=1; 61con=0; 62} 63 64void setup () { 65Serial.begin(9600); 66dht.begin(); 67pinMode(7,OUTPUT); 68pinMode(6,OUTPUT); 69pinMode(10,OUTPUT); 70pinMode(2,OUTPUT); 71digitalWrite(2, LOW); 72digitalWrite(6, LOW); 73digitalWrite(7, LOW); 74digitalWrite(10, LOW); 75Serial.println(F("n[webClient]")); 76if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 77Serial.println(F("Failed to access Ethernet controller")); 78if (!ether.dhcpSetup()) 79Serial.println(F("DHCP failed")); 80ether.printIp("IP: ", ether.myip); 81ether.printIp("GW: ", ether.gwip); 82ether.printIp("DNS: ", ether.dnsip); 83if (!ether.dnsLookup(website)) 84Serial.println("DNS failed"); 85ether.printIp("SRV: ", ether.hisip); 86if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 87Serial.println( "Failed to access Ethernet controller"); 88ether.staticSetup(myip); 89con=0; 90} 91 92void loop () { 93float h = dht.readHumidity(); 94float t = dht.readTemperature(); 95float f = dht.readTemperature(true); 96float hic = dht.computeHeatIndex(t, h, false); 97humidites = h; 98degre =t; 99if (isnan(h) || isnan(t) || isnan(f)) { 100Serial.println("Failed to read from DHT sensor!"); 101return; 102} 103int degree=0; 104int chaleur=0; 105int lampe=0; 106degree = digitalRead(7); 107chaleur = digitalRead(6); 108lampe = digitalRead(10); 109if(emp==1){lampe=1;} 110///////////////////json////////////////////////// 111StaticJsonBuffer<80> jsonBuffer; 112JsonObject& root = jsonBuffer.createObject(); 113root["l"] = degree; 114root["v"] = chaleur; 115root["c"] = lampe; 116root["t"] = t; 117root["hu"] = h; 118root["m"] = hic; 119root["h"] = "H-R"; 120root.printTo(json,sizeof(json)); 121/////////////////////////////////////////////// 122ether.packetLoop(ether.packetReceive()); 123if (millis() > timer) { 124state=0; 125timer = millis() + 5000; 126ether.browseUrl(PSTR("/commands.txt"), "", website, my_callback); 127con +=1; 128} 129if (state==1 && millis() > timers){ 130timers = millis() + 7000; 131ether.browseUrl(PSTR("/arduino.php?contenu="), (char*)json, website, NULL); 132state=0; 133} 134 135if(con>9){ 136digitalWrite(2, HIGH); 137delay(100); 138digitalWrite(2, LOW); 139software_Reset(); 140} 141word len = ether.packetReceive(); 142word pos = ether.packetLoop(len); 143if (pos) 144ether.httpServerReply(homePage()); 145} 146 147void software_Reset() 148{ 149asm volatile (" jmp 0"); 150}
Comments
Only logged in users can leave comments
y-ayachi
0 Followers
•0 Projects
0