Advertisement
Guest User

Untitled

a guest
Jun 29th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. /*
  2. This sketch sends data via HTTP GET requests to data.sparkfun.com service.
  3.  
  4. You need to get streamId and privateKey at data.sparkfun.com and paste them
  5. below. Or just customize this script to talk to other HTTP servers.
  6.  
  7. */
  8.  
  9. #include <ESP8266WiFi.h>
  10. #include <LiquidCrystal.h>
  11. #include <PubSubClient.h>
  12.  
  13.  
  14. const char* ssid = "embarcados";
  15. const char* password = "embarcados";
  16. const char* host = "10.30.152.13";
  17. const char* mqtt_server = "10.30.152.111";
  18. const char* mqtt_user = "embarcados";
  19. const char* mqtt_pass = "embarcados";
  20.  
  21.  
  22. char* command;
  23. char vetor[30];
  24. char* temp;
  25. char* umid;
  26. int intense = 0;
  27.  
  28. IPAddress ip;
  29.  
  30. typedef struct
  31. {
  32. int ip[4];
  33. float temp,umid;
  34. } estrutura;
  35.  
  36. WiFiClient client; //Para servidor vai ser WiFiServer...
  37. PubSubClient pubsubclient(client);
  38.  
  39. void setup() {
  40. Serial.begin(115200);
  41. delay(10);
  42. pinMode(2,OUTPUT); //Pino para o servo motor
  43. pinMode(4, OUTPUT); // led
  44.  
  45. // We start by connecting to a WiFi network
  46.  
  47. Serial.println();
  48. Serial.println();
  49. Serial.print("Connecting to ");
  50. Serial.println(ssid);
  51. setup_wifi();
  52. pubsubclient.setServer(mqtt_server,1883);
  53. pubsubclient.setCallback(callback);
  54. display_data(0,0,ip);
  55.  
  56. }
  57.  
  58. int value = 0;
  59. int i,ldr=0;
  60. estrutura dados;
  61.  
  62.  
  63. void loop() {
  64. delay(5000);
  65. ++value;
  66. ldr = analogRead(12);
  67. Serial.print(ldr);
  68. Serial.println("adc");
  69. intense = (le_adc()*100/1023);
  70. Serial.print(intense);
  71. /* teste led fade
  72. Serial.println("10 % PWM");
  73. analogWrite(4,102);
  74. delay(2000);
  75. TESTE LED FADE
  76. Serial.println("20 % PWM");
  77. analogWrite(4,205);
  78. delay(2000);
  79.  
  80. Serial.println("40 % PWM");
  81. analogWrite(4,410);
  82. delay(2000);
  83.  
  84. Serial.println("70 % PWM");
  85. analogWrite(4,714);
  86. delay(2000);
  87.  
  88. Serial.println("100 % PWM");
  89. analogWrite(4,1024);
  90. delay(2000);
  91. */
  92. Serial.print("connecting to ");
  93. Serial.println(host);
  94.  
  95. // Use WiFiClient class to create TCP connections
  96.  
  97. const int httpPort = 32000;
  98. if (!client.connect(host, httpPort)) {
  99. Serial.println("connection failed");
  100. return;
  101. }
  102.  
  103. ip = WiFi.localIP();
  104. display_data(temp,umid,ip);
  105. for(i=0; i<4;i++){
  106. dados.ip[i] = ip[i];
  107. }
  108.  
  109.  
  110.  
  111. Serial.print(intense);
  112. sprintf(vetor, "%dL",intense);
  113. pubsubclient.publish("ldr_espqt_guilherme",vetor);
  114. pubsubclient.subscribe("servo_esp_guilherme");
  115. pubsubclient.subscribe("temp_esp_guilherme");
  116. pubsubclient.subscribe("umid_esp_guilherme");
  117. pubsubclient.subscribe("led_intense_guilherme");
  118. // This will send the request to the server
  119. //client.print("Ola servidor"); //Manda dados etc para o servidor
  120. unsigned long timeout = millis();
  121. while (client.available() == 0) {
  122. if (millis() - timeout > 5000) {
  123. Serial.println(">>> Client Timeout !");
  124. client.stop();
  125. return;
  126. }
  127. }
  128.  
  129. int i;
  130. for(i=0; i<20;i++){
  131. digitalWrite(2,HIGH);
  132. delay(1);
  133. digitalWrite(2,LOW);
  134. delay(19);
  135. }
  136.  
  137.  
  138. // Read all the lines of the reply from server and print them to Serial
  139. while (client.available()) {
  140. String line = client.readStringUntil('.');
  141. Serial.print(line); //Resposta do servidor
  142. }
  143.  
  144. Serial.println();
  145. Serial.println("closing connection");
  146. }
  147.  
  148. int le_adc(){
  149. return analogRead(0);
  150. }
  151.  
  152. void display_data(char* temp, char* umidade,IPAddress ip){
  153. const int rs = 0, en = 15, d4 = 13, d5 = 12 , d6 = 14, d7 = 16; // temque mudar
  154. LiquidCrystal lcd(rs,en,d4,d5,d6,d7); // config
  155. lcd.begin(16,2);
  156. lcd.setCursor(0,0); // zero 1
  157. lcd.print("IP:");
  158. lcd.print(ip.toString());
  159. lcd.setCursor(0,1);
  160. lcd.print("temp ");
  161. lcd.print(temp);
  162. lcd.print("um ");
  163. lcd.print(umidade);
  164.  
  165. }
  166.  
  167. void setup_wifi(){
  168. /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
  169. would try to act as both a client and an access-point and could cause
  170. network-issues with your other WiFi-devices on your WiFi-network. */
  171. WiFi.mode(WIFI_STA);
  172. WiFi.begin(ssid, password);
  173. ip = WiFi.localIP();
  174. while (WiFi.status() != WL_CONNECTED) {
  175. delay(500);
  176. Serial.print(".");
  177. }
  178.  
  179.  
  180. Serial.println("");
  181. Serial.println("WiFi connected");
  182. Serial.println("IP address: ");
  183. Serial.println(WiFi.localIP());
  184. }
  185.  
  186. void callback(char* topic, byte* payload, unsigned int length) {
  187. Serial.print("Message arrived [");
  188. Serial.print(topic);
  189. Serial.print("] ");
  190.  
  191. if(topic[strlen(topic)-1] == 'T'){
  192. memcpy(topic,temp,sizeof(topic));
  193. }
  194. if(topic[strlen(topic)-1] == 'U'){
  195. memcpy(topic,umid,sizeof(topic));
  196. }
  197. else memcpy(topic,command,sizeof(topic));
  198.  
  199. for (int i = 0; i < length; i++) {
  200. Serial.print((char)payload[i]);
  201. }
  202. Serial.println();
  203.  
  204. memcpy(&command, &payload, sizeof(payload));
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement